package com.demo.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.jfinal.plugin.druid.DruidPlugin;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author: linKe
 * @Date: 2022/4/11 15:11
 * @ClassName: JdbcUtils
 * @Version: 1.0
 * @Description:
 */
public class JdbcUtils {

    private static DataSource ds;
    private static String dirver = "com.mysql.cj.jdbc.Driver";
    private static  String jdbcUrl = "jdbc:mysql://localhost/jfinal_demo?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull";
    private static String user = "root";
    private static String password = "123456";

    private static DruidPlugin dp = new DruidPlugin(jdbcUrl, user, password,dirver);
    static {
        try {
            //获取配置文件
            Properties pro = new Properties();
            InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("demo-config-dev.txt");
            pro.load(is);
            //获取连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static DataSource getDataSource() {
        return ds;
    }
    public static DruidPlugin getDruidPlugin(){
        return dp;
    }
}
